import { Controller } from 'egg';
import { Op } from 'sequelize';

/**
 * 热门搜索词
 * */
export default class KeywordsController extends Controller {

    async helper() {
        const keyword = this.ctx.query.keyword;
        const { query } = this.ctx.validateReq('keywords.list');
        // 过滤空关键字
        if (!query.keyword || query.keyword.length < 1) {
            return this.ctx.success({
                data: [],
            });
        }
        const keywords = await this.ctx.model.Keyword.findAll({
            where: {
                keyword: {
                    [Op.like]: ['%', '%'].join(keyword),
                },
            },
            attributes: ['keyword'],
        });
        this.ctx.success({
            data: keywords.map(i => i.keyword),
        });
    }


   


}
